var _that
var timeId
var app = getApp()
var {keepTwoDecimalFull} = require('../../utils/util')

function carInit(target) {
  _that = target
  _that.setData({
    cartype: 'cake',
    showCart: false,
    showCartCnt: false
  })

  loadCarData()
}

function showDetail() {
  if (_that.data.totalcount <= 0) {
    return
  }
  _that.setData({
    showCart: true
  })
  _that.setData({
    showCartCnt: true
  })
}

function closeDetail() {
  _that.setData({
    showCartCnt: false
  })
  setTimeout(() => {
    _that.setData({
      showCart: false
    })
  }, 300)
}

function toggleDetail() {
  if (_that.data.showCart) {
    closeDetail()
  } else {
    showDetail()
  }
}

function update() {

}

function submitOrder(orderinfo) {
  if (_that.data.address == null || _that.data.address === undefined) {
    app.toast.error('请先添加收货地址！')
    return
  }
  if (_that.data.totalcount <= 0) {
    return
  }
  var orderinfo = {
    mid: _that.data.mid,
    fid: _that.data.fid,
    stationId: _that.data.stationId,
    goodslist: _that.data.goodslist
  }
  wx.navigateTo({
    url: `../neworder/neworder?type=cake&orderinfo=${encodeURIComponent(JSON.stringify(orderinfo))}&address=${encodeURIComponent(JSON.stringify(_that.data.address))}&stationId=${_that.data.stationId}`
  })
}

function startAnimation() {
  _that.setData({
    cartAdded: true
  })

  if (timeId) {
    clearTimeout(timeId)
  }
  timeId = setTimeout(() => {
    _that.setData({
      cartAdded: false
    })
  }, 1000)
}

function handleCount(e) {
  var options, goodsid
  if (e.currentTarget) {
    options = parseInt(e.currentTarget.dataset.options)
    goodsid = e.currentTarget.dataset.goodsid
  }
  else {
    options = e.options
    goodsid = e.goodsid
  }
  var goodslist = _that.data.goodslist
  var goodsidlist = _that.data.goodsidlist
  var totalcount = parseInt(_that.data.totalcount)
  var totalprice = parseFloat(_that.data.totalprice)
  var curIndex = goodsidlist.indexOf(goodsid)

  if (Number(options) === 1) {
    var num = 1
    if (e.num) {
      num = parseInt(e.num)
    }
    totalcount += num
    if (curIndex >= 0) {
      goodslist[curIndex].num = parseInt(goodslist[curIndex].num) + num
      goodslist[curIndex].COUNT = parseInt(goodslist[curIndex].COUNT) + num
      totalprice += parseFloat(goodslist[curIndex].price) * num
      startAnimation()
    }
    else {
      totalprice += parseFloat(e.price) * num
      goodsidlist.push(goodsid)
      goodslist.push({
        goodsid: goodsid,
        ID: goodsid,
        num: num,
        COUNT: num,
        name: e.name,
        shortname: e.shortname,
        price: e.price,
        img: e.img,
        image: e.image,
        icon: e.icon,
        sku: e.sku,
        spec: e.spec,
        size: e.size,
        tips: e.tips
      })
      startAnimation()
    }
  }
  else {
    if (totalcount > 0 && curIndex >= 0) {
      totalcount -= 1
      totalprice -= parseFloat(goodslist[curIndex].price)
      goodslist[curIndex].num = parseInt(goodslist[curIndex].num) - 1
      goodslist[curIndex].COUNT = parseInt(goodslist[curIndex].COUNT) - 1
      if (parseInt(goodslist[curIndex].num) <= 0) {
        goodslist.splice(curIndex, 1)
        goodsidlist.splice(curIndex, 1)
      }
    }
  }

  totalprice = keepTwoDecimalFull(totalprice)
  _that.setData({
    totalcount: totalcount,
    totalprice: totalprice,
    goodslist: goodslist,
    goodsidlist: goodsidlist
  })

  if (totalcount === 0) {
    _that.setData({
      showCart: false
    })
  }
}

function reset(_goodslist = [], _goodsidlist = []) {
  loadCarData(_goodslist, _goodsidlist)
}

module.exports = {
  init: carInit,
  showCar: showDetail,
  closeCar: closeDetail,
  toggleCar: toggleDetail,
  update: update,
  submitOrder: submitOrder,
  handleCount: handleCount,
  reset: reset
}

//------以下方法不外调
function loadCarData(_goodslist = [], _goodsidlist = []) {
  var totalcount = 0
  var totalprice = 0.00
  var num = 0
  for (var i = 0; i < _goodslist.length; i++) {
    num = parseInt(_goodslist[i].num)
    totalcount += num
    totalprice += parseFloat(_goodslist[i].price) * num
  }
  _that.setData({
    totalcount: totalcount,
    totalprice: totalprice,
    goodslist: _goodslist,
    goodsidlist: _goodsidlist
  })
}
